package com.zbf.menu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zbf.api.MenuApi;
import com.zbf.common.entity.ResponseResult;
import com.zbf.dao.RoleDao;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/6/4 14:19
 * 描述: Menu菜单管理
 */

@RestController
@RequestMapping("/menu")
public class MenuController {

    /**
     * 权限相关的接口
     */
    @Autowired
    private MenuApi menuApi;

    @Autowired
    private RoleDao roleDao;

    /**
     * 获取权限菜单的列表
     * @return
     */
    @RequestMapping("/getMenuList")
    public ResponseResult getMenuList(){

        String allMenus = menuApi.getAllMenus();
        ResponseResult responseResult = ResponseResult.getResponseResult();
        if(allMenus!=null&&!allMenus.equals("")){
            JSONArray objects = JSON.parseArray(allMenus);
            responseResult.setResult(objects);
        }else{
            responseResult.setResult(new ArrayList());
        }

        return responseResult;
    }

    /**
     * 角色绑定权限，权限列表
     * @return
     */
    @RequestMapping("/getRoleMenuList")
    public ResponseResult getRoleMenuList(String roleId){

        String allMenus = menuApi.getAllMenus();

        //获取用户的权限信息
        List<Long>  roleMenuId= roleDao.getRoleMenuId(roleId);

        Map<String,Object> map=new HashMap<>();
        ResponseResult responseResult = ResponseResult.getResponseResult();
        if(allMenus!=null&&!allMenus.equals("")){
            JSONArray objects = JSON.parseArray(allMenus);
            map.put("allMenus",objects);
            map.put("roleMenuId",roleMenuId);
            responseResult.setResult(map);
        }else{
            map.put("allMenus",new ArrayList());
            map.put("roleMenuId",new ArrayList());
            responseResult.setResult(map);
        }

        return responseResult;
    }

}
